//
//  EDSPublishDynamicImageVideoView.m
//  DriveSchoolCoach
//
//  Created by TangBin on 2019/7/30.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "EDSPublishDynamicImageVideoView.h"

@interface EDSPublishDynamicImageVideoView ()
@property (nonatomic, strong) UIButton *btnAddImage; ///< 添加图片
@property (nonatomic, assign) CGRect btnAddImageSrcRect; ///< 最初的位置
@property (nonatomic, strong) NSMutableArray *imageList; ///< 图片数组
@property (nonatomic, strong) UIImageView *videoImgView; ///< 视图第一帧的图片
@property (nonatomic, strong) UIImageView *playImgView; ///< 视频标记

@property (nonatomic, assign) float imageWidth;
@property (nonatomic, assign) float imageHeight;

/// 数量
@property (nonatomic, assign) NSInteger imageVideoNum;

- (void)initWithBtnAddImage;

@end

@implementation EDSPublishDynamicImageVideoView


- (instancetype)initWithFrame:(CGRect)frame imageVideoNum:(NSInteger)imageVideoNum imageWidth:(float)imageWidth imageHeight:(float)imageHeight {
    _imageVideoNum = imageVideoNum;
    _imageWidth = imageWidth;
    _imageHeight = imageHeight;
    return [self initWithFrame:frame];
}

- (void)initWithVar
{
    _imageList = [[NSMutableArray alloc] initWithCapacity:0];
}

- (void)initWithSubView
{
    self.backgroundColor = [UIColor colorWithHexString:@"#FAFAFA"];
    [self initWithBtnAddImage];
    
    [self initWithImageList];
    
    [self initWithVideoImgView];
}

- (void)initWithBtnAddImage
{
//    float width = (kScreenWidth - 16 * 2 - 4 * 3) / 4.0;
    CGRect frame = CGRectMake(0, 0, self.imageWidth, self.imageHeight);
    self.btnAddImageSrcRect = frame;
    _btnAddImage = [UIButton createWithButton:self frame:frame font:FONTSIZE(15) action:nil propertiesBlock:^(UIButton * _Nonnull button) {
        [button setImage:[UIImage imageNamed:@"ic_upload_camera_image"] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor colorWithHexString:@"#F6F7FB"];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
//        button.layer.borderColor = [UIColor colorWithHexString:@"#DFE1E9"].CGColor;
//        button.layer.borderWidth = 1;
    }];
//    _btnAddImage.backgroundColor = [UIColor redColor];
}

- (void)initWithImageList
{
    CGRect frame = self.btnAddImageSrcRect;
    float midSpace = (self.width - self.imageWidth * 3) * 0.5;
    for (int i=0; i<self.imageVideoNum; i++)
    {
        if (i != 0)
        {
            EDSPublishDynamicImageView *tmpView = self.imageList.lastObject;
            if (i % 3 == 0)
            {
                frame.origin.x = 0;
                frame.origin.y = tmpView.bottom + 4;
            }
            else
            {
                frame.origin.x = tmpView.right + midSpace;
            }
        }
        EDSPublishDynamicImageView *imgView = [[EDSPublishDynamicImageView alloc] initWithFrame:frame];
        imgView.layer.cornerRadius = 8;
        imgView.layer.masksToBounds = YES;
        imgView.tag = i + 100;
//        imgView.backgroundColor = [UIColor colorWithRandom];
        imgView.hidden = YES;
        [self addSubview:imgView];
        [self.imageList addObject:imgView];
        @weakify(self);
        imgView.deleteImageBlock = ^(NSInteger index) {
            @strongify(self);
            if (self.deleteImageBlock)
            {
                self.deleteImageBlock(index - 100);
            }
        };
        
    }
}

- (void)initWithVideoImgView
{
    CGRect frame = CGRectMake(0, 0, 156, 96);
    _videoImgView = [[UIImageView alloc] initWithFrame:frame];
    [self addSubview:_videoImgView];
    _videoImgView.hidden = YES;
    
    UIImage *image = [UIImage imageNamed:@"com_play_big_ic"];
    _playImgView = [[UIImageView alloc] init];
    _playImgView.image = image;
    _playImgView.size = image.size;
    [_videoImgView addSubview:_playImgView];
//    _playImgView.center = CGPointMake(_videoImgView.width * 0.5, _videoImgView.height * 0.5);
    @weakify(self);
    _videoImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self);
        if (self.touchVideoImgViewblock)
        {
            self.touchVideoImgViewblock();
        }
    }];
    [_videoImgView addGestureRecognizer:tap];
}

@end
